<script lang="ts" setup>
import { ref, onMounted, reactive, onBeforeUnmount } from "vue";
import { Upload, Pointer } from "@element-plus/icons-vue";
import ExportXlsxForm, { ExportXlsxExpose } from "./modules/ExportXlsx.vue";
import { isArrayEmpty } from "@/utils/index";
import { getLocalSubjectData } from "@/utils/question-test";
import { SubjectTypeEnum } from "@/enum/question-test/QuestionTestEnum";
import { useRouter } from "vue-router";
import * as QuestionTestType from "./types";

const router = useRouter();
const text = ref<QuestionTestType.BtnTextType>("上传xlsx文件");
const ExportXlsxRef = ref<ExportXlsxExpose | null>();
const drawer = ref(false);
const form = reactive<QuestionTestType.FormType>({
  mode: SubjectTypeEnum.THREE_CHOICE,
});

const body = ref<HTMLBodyElement | null>();
onMounted(() => {
  text.value = hasData() ? "前往刷题" : "上传xlsx文件";
  // 获取body
  body.value = document.querySelector("body");
  // 设置overflow-hidden
  body.value?.setAttribute("style", "overflow: hidden");
});

onBeforeUnmount(() => {
  body.value?.setAttribute("style", "overflow: auto");
});

function handleExportXlsx() {
  if (!hasData()) {
    // 显示弹窗
    return ExportXlsxRef.value?.show(true);
  }
  drawer.value = true;
}
// 提交表单
function handleSubmit() {
  router.push({
    path: "/question/test",
    query: {
      mode: form.mode,
    },
  });
}

function handleExportXlsxSuccess(done: boolean) {
  if (done) {
    text.value = "前往刷题";
  }
}

// 是否有数据，true有数据，false没有数据
function hasData() {
  return !isArrayEmpty(getLocalSubjectData());
}

defineOptions({
  name: "QuestionTest",
});
</script>

<template>
  <div class="question-test">
    <!-- 背景图 -->
    <img src="./assets/imgs/question-bg.jpg" class="question-bg" />

    <div class="content">
      <div class="title">中国近代史</div>
      <div class="start-btn">
        <el-button
          round
          class="upload-btn"
          @click="handleExportXlsx"
          type="primary"
          :icon="hasData() ? Pointer : Upload"
          >{{ text }}</el-button
        >
      </div>
    </div>

    <el-form :model="form">
      <el-form-item>
        <!-- 选择题型 -->
        <el-drawer
          v-model="drawer"
          class="question-type-drawer"
          title="🍊选择题题型"
        >
          <el-radio-group v-model="form.mode">
            <el-radio :value="SubjectTypeEnum.ALL">全题型</el-radio>
            <el-radio :value="SubjectTypeEnum.SINGLE">单选题</el-radio>
            <el-radio :value="SubjectTypeEnum.MULTIPLE">多选题</el-radio>
            <el-radio :value="SubjectTypeEnum.JUDGE">判断题</el-radio>
            <el-radio :value="SubjectTypeEnum.TWO_CHOICE">二选题</el-radio>
            <el-radio :value="SubjectTypeEnum.THREE_CHOICE">三选题</el-radio>
          </el-radio-group>

          <template #footer>
            <div style="flex: auto; margin-bottom: 120px;">
              <el-button size="large" type="primary" @click="handleSubmit"
                >🍀BEGIN</el-button
              >
              <el-button size="large" @click="drawer = false"
                >🥀GIVEUP</el-button
              >
            </div>
          </template>
        </el-drawer>
      </el-form-item>
    </el-form>

    <ExportXlsxForm ref="ExportXlsxRef" @on-sucess="handleExportXlsxSuccess" />
  </div>
</template>

<style lang="scss" scoped>
.question-test {
  height: 100%;
  .question-bg {
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-fit: cover;
    position: fixed;
    inset: 0;
    z-index: -1;
  }

  .content {
    height: 100vh;
    display: grid;
    grid-auto-columns: 1fr 1fr;
    .title {
      display: flex;
      align-items: center;
      justify-content: center;
      text-align: center;
      color: #fff;
      font-weight: bold;
      font-size: 3rem;
      margin-bottom: 30px;
    }
    .start-btn {
      width: 100%;
      display: flex;
      justify-content: center;
      .upload-btn {
        height: 40px;
        width: calc(100% - 80px);
      }
    }
  }

  :deep(.question-type-drawer) {
    width: 70% !important;
  }
}
</style>
